// components/load-list/load-list.js
Component({
    /*
    * 接收外部样式
    * */
    options: {
        addGlobalClass: true,
        multipleSlots: true
    },
    /**
     * 组件的属性列表
     */
    properties: {
        page: { //页数
            type: Number,
            value: 1
        },
        pageSize: { //每页个数  默认20个
            type: Number,
            value: 20
        },
        total: { //总条数
            type: Number,
            value: 0
        },
        loading: { //是否加载  默认加载
            type: Boolean,
            value: false
        },
        loadingText: { //加载中提示
            type: String,
            value: "加载中..."
        },
        finishedText: { //加载结束提示内容
            type: String,
            value: "没有更多了"
        },
        useFinishedSlot: { //是否打开 完成加载的提示插槽
            type: Boolean,
            value: false,
        },
        error: { //加载失败
            type: Boolean,
            value: false
        },
        errorText: { //加载错误提示内容
            type: String,
            value: "加载失败，点击重新加载"
        },
        reset: { //刷新 重置
            type: Boolean,
            value: false
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _status: "default", //加载状态   default\loading\err\finished
        _showErrIcon: false //是否在加载内容失败时候显示图标
    },


    /*
    * 数据监听
    * */
    observers: {
        "loading": function (loading) {
            if (loading) {
                if (this.data._status === "default" && this._checked()) {
                    let page = this.data.page;
                    let pageSize = this.data.pageSize;
                    this.setData({_status: "loading"});
                    this.triggerEvent("loadList", { //组件接收函数写入数据
                        status: "success",
                        paging: {page, pageSize}
                    }, {});
                    this.data.page += 1;
                }
            } else {
                if (!this._checked()) {
                    this.setData({_status: "finished"})
                }
            }
        },
        "reset": function (reset) {
            if (reset) {
                /*初始化数据*/
                if (this.data._status !== "loading") {
                    let pageSize = this.data.pageSize;
                    this.data.page = 1;
                    this.data.total = 0;
                    this.data.loading = false;
                    this.setData({_status: "loading"})
                    this.triggerEvent("loadList", {
                        status: "success",
                        paging: {page: 1, pageSize}
                    }, {}); //组件接收函数写入数据
                    this.data.page += 1;
                }
            }
        },
        "total": function () {
            if (this.data.page > 0) {
                if (this._checked()) {
                    this.setData({_status: "default"});
                } else {
                    this.setData({_status: "finished"});
                }
            }
        },
        "error": function (error) {
            if (error) {
                this.data.page -= 1;
                this.setData({
                    _status: "error",
                    _showErrIcon: this.data.page === 1
                });
            }
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {

        /*
        * 验证数据加载进度
        * false 加载完毕
        * true  未加载完毕
        * */
        _checked: function () {
            let page = this.data.page,
                pageSize = this.data.pageSize,
                total = this.data.total;
            if (page === 1) { //第一次请求 直接抛出执行
                return true
            } else {
                page -= 1;
                return total > page * pageSize
            }
        },

        /*
        * 点击加载失败提示，重新加载
        * */
        bindErrTip: function () {
            let page = this.data.page;
            let pageSize = this.data.pageSize;
            this.setData({_status: "loading"});
            this.triggerEvent("loadList", {
                status: "error",
                paging: {page, pageSize}
            }, {}); //组件接收函数写入数据
            this.data.page += 1;
        }
    }
})
